

class DResMgr {
    public static RES_READY = 0;
    public static RES_LOADING = 1;
    public static RES_LOADED = 2;

    private static _map = {};
    public static async load(loads:string[], onProcess?:Function, thisObj?:any):Promise<any[]>{
        let loaded = [];
        for(var i = 0; i < loads.length; i ++){
            loaded.push(await this.loadOne(loads[i]))
            if(onProcess){ onProcess.call(thisObj, loads.length, i); }
        }
        return loaded;
    }

    public static async loadOne(path:string):Promise<any>{
        let r = this._map[path];
        if(!r){
            r = {
                ref:0,
                path:path,
                status:0,
                res:null,
            }
            this._map[path] = r
        }
        r.ref ++;
        if(r.status != DResMgr.RES_LOADED){
            r.res = await RES.getResByUrl(r.path);
            r.status = DResMgr.RES_LOADED;
        }
        return r.res;
    }

    public static release(paths:string[]){
        paths.forEach((v)=>{
            let r = this._map[v];
            if(!r){ return; }
            r.ref --;
            if(r.ref <= 0){
                RES.destroyRes(v);
                delete this._map[v];
            }
        });
    }
}
